package com.lb.jwitter.action.report;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiam;
import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiamService;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXALop;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;
import com.lb.jwitter.service.TblQLDTTXALopService;
import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienKhongDangKyMonHocReportAction{
    private Message msg; 
    public Message getMsg() { return msg; }

   
    private Long hocKyId;
    private Long lopId;
    
    private String report;    
    
    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;
    private TblQLDTTXALopService tblQLDTTXALopService;
    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService;
    
    
    private TblQLDTTXAHocVienPhieuDangKyMonHocService tblQLDTTXAHocVienPhieuDangKyMonHocService;
    
    
    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams;
    
    private List<TblQLDTTXAHocVienPhieuDangKyMonHoc> tblQLDTTXAHocVienPhieuDangKyMonHocs;
    private List<TblQLDTTXALop> tblQLDTTXALops;


  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_hocPhi", "xem")){ 
        return "noprivilege"; 
    } 
 
        //tblQLDTTXADMChiNhanhService = (TblQLDTTXADMChiNhanhService) context.getBean("tbl_QLDTTXA_DM_ChiNhanhClient");
        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
        tblQLDTTXALopService = (TblQLDTTXALopService) context.getBean("tbl_QLDTTXA_LopClient");
        
//        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
//        tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 


        msg = new Message(); 
        
        
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        } 
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 20, 1);
        tblQLDTTXALops = tblQLDTTXALopService.findAlltblQLDTTXALop(0,0 );
        
        
        return Action.SUCCESS;
	 }catch(Exception e){ 
	     System.out.println("Loi Report tblQLDTTXABienNhanHoSo Action"); 
	     e.printStackTrace(); 
	     return Action.ERROR; 
	}
}
 
 
 
 
 public String execute() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_BienNhanHoSo", "xem")){ 
         return "noprivilege"; 
     } 
  
         tblQLDTTXAHocVienPhieuDangKyMonHocService = (TblQLDTTXAHocVienPhieuDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_PhieuDangKyMonHocClient"); 
 
         tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient");
         
         msg = new Message(); 
         List<ConditionReport> cons = new ArrayList<ConditionReport>();
         
         
         //lay cac thong tin dkmh cua hoc vien
         cons.clear();
         if(hocKyId != null && !hocKyId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocKyId.id");
             con.setValue1(hocKyId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(lopId != null && !lopId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.lopId.id");
             con.setValue1(lopId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.trangThaiId.id");
             con.setValue1(new Long(1));
             con.setOperator("eq");
             cons.add(con);	
         }
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.ten");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.hoTen");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 

         tblQLDTTXAHocVienPhieuDangKyMonHocs = tblQLDTTXAHocVienPhieuDangKyMonHocService.findtblQLDTTXAHocVienPhieuDangKyMonHocByCondition(cons, 0, 0);
	      
         cons.clear();         
         if(lopId != null && !lopId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("lopId.id");
             con.setValue1(lopId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         // trang thai khac buoc thoi hoc (2) va da tot nghiep (3)
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("trangThaiId.id");
             con.setValue1(new Long(2));
             con.setOperator("ne");
             cons.add(con);	
         }
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("trangThaiId.id");
             con.setValue1(new Long(3));
             con.setOperator("ne");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("ten");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hoTen");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         List<TblQLDTTXAHocVien> tblQLDTTXAHocViens = tblQLDTTXAHocVienService.findtblQLDTTXAHocVienByCondition(cons, 0, 0);
	     	      
		  //sinh report
		  int colNo = 0;
		  
		  report ="";
		  int stt = 0;		  
		  
		  for(TblQLDTTXAHocVien hocVien : tblQLDTTXAHocViens){
			  boolean coDKMH = false;
			  for(TblQLDTTXAHocVienPhieuDangKyMonHoc hocVienPhieuDangKyMonHoc : tblQLDTTXAHocVienPhieuDangKyMonHocs){ 
				   
				  if (hocVienPhieuDangKyMonHoc.getHocVienId().getId().equals(hocVien.getId())){
					  coDKMH = true;
				  }
			      
			      
			      
			  }
			  if(!coDKMH){
				  stt++;
			      report += "<tr class='tableitem' > "; 
			      //stt
			      report += "<TD>" +stt+ "</TD>"; 
			      //mshv
			      report += "<td>" + (hocVien.getMSHV() == null ? "" : hocVien.getMSHV())+ "</td> ";
			      //lop
			      report += "<td>" +hocVien.getLopId().getTen()+ "</td> ";
			      //ho ten
			      report += "<td>" +hocVien.getHoTen()+ "</td> ";
			      report += "<td>" +hocVien.getTen()+ "</td> ";
			      //ngay sinh
			    //ngay sinh
			      try{
				      report += "<td>" 
				    	  +hocVien.getHoSoId().getNgaySinh().getDay()+"/"
				    	  +hocVien.getHoSoId().getNgaySinh().getMonth()+"/"
				    	  +hocVien.getHoSoId().getNgaySinh().getYear()+ "</td>";
				      
			      }catch(Exception e){
			    	  report += "<td></td>"; 
			            
			      }
			      
			  }
		  }

		  
  
		  return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXABienNhanHoSo Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public String getReport() {
	return report;
}

public void setReport(String report) {
	this.report = report;
}


public Long getHocKyId() {
	return hocKyId;
}
public void setHocKyId(Long hocKyId) {
	this.hocKyId = hocKyId;
}
public Long getLopId() {
	return lopId;
}
public void setLopId(Long lopId) {
	this.lopId = lopId;
}

public void setMsg(Message msg) {
	this.msg = msg;
} 
public List<TblQLDTTXALop> getTblQLDTTXALops() {
	return tblQLDTTXALops;
}
public List<TblQLDTTXAHocKyTrongNam> getTblQLDTTXAHocKyTrongNams() {
	return tblQLDTTXAHocKyTrongNams;
}
 } 
 
